<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .box {
      width: 450px;

      margin: 50px;
      display: flex;
      flex-direction: column;

      position: relative;
    }

    .box>.show {
      width: 400px;
      height: 400px;
      border: 1px solid #333;
      position: relative;
    }

    .box>.show>.mask {
      width: 200px;
      height: 200px;
      background-color: yellow;
      opacity: .4;
      position: absolute;
      top: 0px;
      left: 0px;
      display: none;
    }

    .box>.list {
      width: 100%;
      flex: 1;

      display: flex;
      justify-content: flex-start;
      align-items: center;

      box-sizing: border-box;
      padding: 20px;
    }

    .box>.list>p {
      width: 54px;
      height: 54px;
      border: 1px solid #333;
      margin-right: 10px;
    }

    .box>.list>p.active {
      border-color: red;
    }

    .box>.enlarge {
      width: 400px;
      height: 400px;
      position: absolute;
      top: 0;
      left: 101%;
      background-image: url(../images/1.big.jpg);
      background-repeat: no-repeat;
      background-size: 800px 800px;
      background-position: 0 0;
      display: none;
    }
  </style>
</head>

<body>

  <div class="box">
    <div class="show">
      <img src="../images/1.jpg" alt="">
      <div class="mask"></div>
    </div>
    <div class="list">
      <p class="active">
        <img src="../images/1.small.jpg" showImg="../images/1.jpg" enlargeImg="../images/1.big.jpg" alt="">
      </p>
      <p>
        <img src="../images/2.small.jpg" showImg="../images/2.jpg" enlargeImg="../images/2.big.jpg" alt="">
      </p>
    </div>
    <div class="enlarge"></div>
  </div>
  <script>
    /* 
    分析需求：1、鼠标移入 遮罩层和 放大图都需要显示，鼠标移除的时候 遮罩层和放大图隐藏
             2、鼠标在移动的时候遮罩层要跟着鼠标动 （鼠标的中间动）（边界问题）
             3、放大图也要按照左边比例来移动
             4、鼠标移到小图上可以切换图片
     */
    function Enlarge( containerEle ){       //定义一个Enlarge类，传入一个容器
        this.containerEle = containerEle ;    //获取容器
        this.showEle = this.containerEle.querySelector(".show");
        this.showImgEle = this.containerEle.querySelector(".show img");
        this.maskEle = this.containerEle.querySelector(".mask");
        this.listEles = this.containerEle.querySelectorAll(".list > p");
        this.enlargeEle = this.containerEle.querySelector(".enlarge");
        this.w = this.containerEle.offsetLeft ; 
        this.h = this.containerEle.offsetTop ; 
        this.move();
        this.tab();
    }    
    Enlarge.prototype.tab = function (){
      const _that = this ;
      this.listEles.forEach((listEle,key)=>{
        listEle.onclick = function (){
          _that.listEles.forEach((item,index)=>{
            item.classList.remove("active");
          });
          this.classList.add("active");
          _that.enlargeEle.style.backgroundImage = `url(../images/${key+1}.big.jpg)` ;
          _that.showImgEle.src = `../images/${key+1}.jpg` ;
        }
      });
    }
    Enlarge.prototype.move = function (){
      const _that = this ; 
      this.showEle.onmouseenter = function (){
        _that.maskEle.style.display = "block" ;
        _that.enlargeEle.style.display = "block" ;
      }
      this.showEle.onmouseleave = function (){
        _that.maskEle.style.display = "none" ;
        _that.enlargeEle.style.display = "none" ;
      }
      this.showEle.onmousemove = function (event){
        let e = event || window.event ; 
        _that.maskWidth = _that.maskEle.offsetWidth ; 
        _that.maskHeight = _that.maskEle.offsetHeight ; 
        let mouseX = e.clientX ; 
        let mouseY = e.clientY ;
        let x = mouseX - _that.w - _that.maskWidth/2 ; 
        let y = mouseY - _that.h - _that.maskHeight/2 ; 
        if( x < 0 ){
          x = 0 ;
        }
        if( x > _that.showEle.offsetWidth - _that.maskWidth ){
          x = _that.showEle.offsetWidth - _that.maskWidth ; 
        }
        if( y < 0 ){
          y = 0 ; 
        }
        if( y > _that.showEle.offsetHeight - _that.maskHeight ){
          y = _that.showEle.offsetHeight - _that.maskHeight  ;
        }
        _that.maskEle.style.left = x + "px" ; 
        _that.maskEle.style.top = y + "px" ; 
        let arr = (getComputedStyle(_that.enlargeEle,"null")["background-size"]).split(" ") ; 
        let xx = parseInt(arr[0]) * x / _that.showEle.offsetWidth ; 
        let yy = parseInt(arr[1]) * y / _that.showEle.offsetHeight ; 
        _that.enlargeEle.style.backgroundPosition = `-${xx}px -${yy}px` ;
      }
    }
    let boxEle = document.querySelector(".box");
    console.log(boxEle);
    new Enlarge(boxEle);
  </script>


</body>

</html>